package com.usian.controller;

import com.sun.rowset.internal.Row;
import com.usian.feign.ItemFeign;

import com.usian.pojo.TbItem;
import com.usian.pojo.TbItemCat;
import com.usian.pojo.TbItemParam;
import com.usian.utils.PageResult;
import com.usian.utils.QiniuUtils;
import com.usian.utils.Result;
import org.apache.catalina.mapper.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@RestController
public class TbItemController {
    @Autowired
    ItemFeign itemFegin;

    @RequestMapping("backend/item/selectTbItemAllByPage")
    public Result selectTbItemAllByPage(@RequestParam(value = "page",defaultValue = "1") String page, @RequestParam(value = "rows",defaultValue = "2") String rows){
        PageResult pageResult =  itemFegin.selectTbItemAllByPage(page, rows);

        if (pageResult.getResult() != null && pageResult.getResult().size() > 0) {
            return Result.ok(pageResult);
        }
        return Result.error("查无结果");
    }

    @RequestMapping("/backend/itemCategory/selectItemCategoryByParentId")
    public Result selectItemCategoryByParentId(@RequestParam(value = "id",defaultValue = "0") Integer id){
        List<TbItemCat> list=itemFegin.selectItemCategoryByParentId(id);
        if (list==null || list.size()==0){
            return Result.error("查无结果");
        }
        return Result.ok(list);
    }
    @RequestMapping("/backend/itemParam/selectItemParamByItemCatId/{itemCatId}")
    public Result selectItemParamByItemCatId(@PathVariable("itemCatId") Integer itemCatId){
       TbItemParam  tbItemParam= itemFegin.selectItemParamByItemCatId(itemCatId);
       return Result.ok(tbItemParam);
    }
    @PostMapping("/backend/item/insertTbItem")
    public Result insertTbItem(TbItem tbItem, String desc, String itemParams) {
       Integer num= itemFegin.insertTbItem(tbItem,desc,itemParams);
       if (num==3){
           return Result.ok(num);
       }
       return Result.error("添加失败");
    }
    @RequestMapping("/backend/item/preUpdateItem")
    public Result preUpdateItem(Long itemId){
       Map<String,Object> map= itemFegin.preUpdateItem(itemId);
       if (map==null){
           return Result.error("查无结果");
       }
        return Result.ok(map);
    }

    @RequestMapping("/backend/item/deleteItemById")
    public Result deleteItemById(Long itemId) {
        Integer count = itemFegin.deleteItemById(itemId);
        if (count == 3) {
            return Result.ok();
        }
        return Result.error("删除失败");
    }
    @RequestMapping("/backend/item/updateTbItem")
    public Result updateTbItem(TbItem tbItem,@RequestParam("desc") String desc,@RequestParam("itemParams") String itemParams){
        Integer num=itemFegin.updateTbItem(tbItem,desc,itemParams);
        if(num==3){
            return Result.ok();
        }
        return Result.error("修改失败");
    }
    private static final List<String> CONTENT_TYPES = Arrays.asList("image/jpeg", "image/gif");

    @RequestMapping("/file/upload")
    public Result upload(MultipartFile file){

        try {

            //重新命名
            String filenameOld = file.getOriginalFilename();
            String filename = UUID.randomUUID().toString()+filenameOld.substring(filenameOld.lastIndexOf("."));
            //图片上传
            QiniuUtils.upload2Qiniu(file.getBytes(),filename);

/*            String path = request.getSession().getServletContext().getRealPath("/");
            String filenameOld = imgFile.getOriginalFilename();
            imgFile.transferTo(new File(path+"/"+filenameOld));*/


//            上传的时候需要把图片文件名添加setA  redis 的set类型
//            setA : 1.jpg 2.jpg 3.jpg
            return Result.ok(filename);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("失败");
        }
    }
}
